<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'web_site/threads_manager.class.php');


require_once($WSC->get_site_inc_dir().'modules/web_site_chocolate/web_site_chocolate.class.php');


class Web_Site_Caramel extends Web_Site_Chocolate {
	public function init() {
		parent::init();

		$this->init_threads_manager();
	}


	private function init_threads_manager() {
		$svm = $this->get_svm();
		$context = $this->get__context();

		if ($svm->is_global_var_registered(THREADS_MANAGER_NAME)) {
			//already exists
			$tm = $svm->get_global_var(THREADS_MANAGER_NAME);

			$tm->process($context);
		} else {
			//new
			$svm->register_global_var(THREADS_MANAGER_NAME);
			$tm = new Threads_Manager(THREADS_MANAGER_NAME);
			$tm->process($context);
			$svm->set_global_var(THREADS_MANAGER_NAME, $tm);
		}
	}



	public function run(Web_Page $page, Web_Context $context) {
		$ret = parent::run($page, $context);

		$svm = $this->get_svm();
		$tm = $svm->get_global_var(THREADS_MANAGER_NAME);

		$tm->save_snapshot();

		return $ret;
	}


	public function run_other(Web_Ctrl $object , Web_Context $context) {
		$ret = parent::run_other($object, $context);

		$svm = $this->get_svm();
		$tm = $svm->get_global_var(THREADS_MANAGER_NAME);

		$tm->save_snapshot();

		return $ret;
	}


	public function get_tvm() {
		$svm = $this->get_svm();

		$tm = $svm->get_global_var(THREADS_MANAGER_NAME);
		$tvm = $tm->get_current_thread_vm();

		return $tvm;
	}


	public function query_tvm() {
		$svm = $this->get_svm();

		$tm = $svm->get_global_var(THREADS_MANAGER_NAME);
		$tvm = $tm->get_current_thread_vm();

		return $tvm;
	}
}


